package com.alibaba.user.service.impl;

import com.alibaba.basic.businessexception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.Md5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.dto.UserDto;
import com.alibaba.user.mapper.LogininfoMapper;
import com.alibaba.user.mapper.UserMapper;
import com.alibaba.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void phoneRegister(UserDto userDto) {
        // 获取userDto里面的所有值
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String pwd = userDto.getPwd();
        String rePwd = userDto.getRePwd();

        // 空置校验
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)
                ||StringUtils.isEmpty(pwd) || StringUtils.isEmpty(rePwd)){
            throw  new BusinessException("信息不能为空!!!");
        }

        // 检验密码是否相同
        if (!pwd.equals(rePwd)){
            throw  new BusinessException("两次密码不一致!!!");
        }

        // 检验短信验证码是否过期和正确
        // 1.获取redis中奖的验证码
        Object o = redisTemplate.opsForValue().get("register:" + phone);
        if(o==null){
            throw  new BusinessException("手机验证码过期!!!");
        }

        if (o.toString().split(":")[0].equals(phoneCode)){
            throw  new BusinessException("手机验证码错误!!!!");
        }

        // 2.添加到数据库,将useDto对象转换成User，运用copyPropreties方法
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);

        // 先添加logininfo,让他id自增
        logininfoMapper.add(logininfo);
        // 在设置user的logininfoid
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }
    // 对象互相转换方法
    private User userDto2User(UserDto userDto){
        // 先设定一个新的User变量来装
        User user = new User();
        // 用户手机注册 默认name是手机号
        user.setUsername(userDto.getPhone());

        // 手机号就是userDto手机号
        user.setPhone(userDto.getPhone());
        // 随机生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 加密
        String md5Pwd = Md5Utils.encrypByMd5(userDto.getPwd() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

    private Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        // 按照同名原则赋值
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        return logininfo;
    }

    @Override
    @Transactional
    public void add(User user) {
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        super.add(user);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        User user = userMapper.findById(id);
        logininfoMapper.remove(user.getLogininfoId());
        super.remove(id);
    }

    @Override
    @Transactional
    public void update(User user) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        super.update(user);
        // 先获取老的对象
        Long logininfoId = userMapper.findById(user.getId()).getLogininfoId();

        // 再根据logininfoId查询
        Logininfo logininfo = logininfoMapper.findById(logininfoId);
        BeanUtils.copyProperties(user,logininfo);
        logininfoMapper.update(logininfo);
    }
}
